//
//  GWEmotionTool.m
//  新浪微博
//
//  Created by qianfeng on 16/2/21.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWEmotionTool.h"
#import "MJExtension.h"
#import "GWEmotion.h"
#define EmotionPATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"recentEmotion.data"]
/**
 *  默认表情
 */
static NSArray *defaultEmotions;
/**
 *  Emoji表情
 */
static NSArray *emojiEmotions;
/**
 *  小浪花表情
 */
static NSArray *xlhEmotions;

/**
 *  最近使用表情
 */
static NSMutableArray *recentEmotions;

@implementation GWEmotionTool

+(NSArray *)defaultEmotions
{
    if (defaultEmotions ==nil) {
        NSString *path =[[NSBundle mainBundle] pathForResource:@"EmotionIcons/default/info.plist" ofType:nil];
        defaultEmotions =[GWEmotion objectArrayWithFile:path];
        [defaultEmotions makeObjectsPerformSelector:@selector(setFilePath:) withObject:@"EmotionIcons/default"];
    }
    return defaultEmotions;
}
+(NSArray *)emojiEmotions
{
    if (emojiEmotions ==nil) {
        NSString *path =[[NSBundle mainBundle] pathForResource:@"EmotionIcons/emoji/info.plist" ofType:nil];
        emojiEmotions =[GWEmotion objectArrayWithFile:path];
        [emojiEmotions makeObjectsPerformSelector:@selector(setFilePath:) withObject:@"EmotionIcons/emoji"];
    }
    return emojiEmotions;
}
+(NSArray *)xlhEmotions
{
    if (xlhEmotions ==nil) {
        NSString *path =[[NSBundle mainBundle] pathForResource:@"EmotionIcons/lxh/info.plist" ofType:nil];
        xlhEmotions =[GWEmotion objectArrayWithFile:path];
        [xlhEmotions makeObjectsPerformSelector:@selector(setFilePath:) withObject:@"EmotionIcons/lxh"];
    }
    return xlhEmotions;
}

+(NSArray *)recentEmotions
{
    if (recentEmotions==nil) {
        recentEmotions =[NSKeyedUnarchiver unarchiveObjectWithFile:EmotionPATH];
        if (recentEmotions==nil) {
            recentEmotions =[NSMutableArray array];
        }
    }
    return recentEmotions;
}

+(void)addEmotionWithEmotion:(GWEmotion *)emotion
{
    [self recentEmotions];
    [recentEmotions removeObject:emotion];
    [recentEmotions insertObject:emotion atIndex:0];
    [NSKeyedArchiver archiveRootObject:recentEmotions toFile:EmotionPATH];
}
+(GWEmotion *)emotionWitonDsec:(NSString *)dsec
{
    if(dsec==nil)return nil;
   __block GWEmotion *emtion =nil;
    [defaultEmotions enumerateObjectsUsingBlock:^(GWEmotion *obj, NSUInteger idx, BOOL *stop) {
        if ([dsec isEqualToString:obj.chs]||[dsec isEqualToString:obj.cht]) {
            emtion = obj;
            *stop =YES;
        }
    }];
    if(emtion)return emtion;
    [xlhEmotions enumerateObjectsUsingBlock:^(GWEmotion *obj, NSUInteger idx, BOOL *stop) {
        if ([dsec isEqualToString:obj.chs]||[dsec isEqualToString:obj.cht]) {
            emtion = obj;
            *stop =YES;
        }
    }];
    return emtion;
}
@end
